﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "User management"
		PageController.AuthorizedRoles =  Array("admin")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>User management</H2>
	

	
	<%Page.Execute%>	
	<Span Class="Caption">User Edit<HR></Span>
	

	<%panel%>
	
	
<%End Function%>

<%  '
	Dim txtUsername
	Dim txtPassword
	Dim txtRePassword
	Dim txtFirstname
	Dim txtLastname
	Dim txtEmail
	Dim txtPhone
	Dim txtAddress
	Dim chkStatus
	Dim chkDelete
	Dim chkRoles
	
	Dim Panel

	Dim mValidation

	Dim cmdSave	
	Dim recordID
	
		
	Public Function Page_Init()
		recordID = 0  'Init
		
		
		Set  txtUsername	= New_ServerTextBoxEX("txtUsername",20,20)
		Set  txtPassword	= New_ServerTextBoxEX("txtPassword",20,20)
		Set  txtRePassword	= New_ServerTextBoxEX("txtRePassword",20,20)
		Set  txtFirstname	= New_ServerTextBoxEX("txtFirstname",20,20)
		Set  txtLastname	= New_ServerTextBoxEX("txtLastname",20,20)
		Set  txtEmail	= New_ServerTextBoxEX("txtEmail",20,20)
		Set  txtPhone	= New_ServerTextBoxEX("txtPhone",20,20)
		Set  txtAddress	= New_ServerTextBoxEX("txtAddress",20,20)
		
		Set  chkStatus	= New_ServerCheckBox("chkStatus")
		Set  chkDelete	= New_ServerCheckBox("chkDelete")
		Set chkRoles = new_serverCheckBoxList("chkRoles")
		
		Set cmdSave = New_ServerLinkButton("cmdSave")		
		
		Set mValidation = New ServerValidationSummary
		Set panel = New_ServerPanel("Panel",700,500)
		
	End Function

	Public Function Page_Controls_Init()		
		cmdSave.Text = "Save"
		
		chkStatus.Checked = true
		txtPassword.Mode =2
		txtRePassword.Mode =2
		panel.Mode = 2
		'panel.OverFlow = "auto" 'scroll?
		panel.PanelTemplate = "RenderPanel"
		
		chkRoles.RepeatColumns = 3
		chkRoles.RepeatLayOut = 1
		chkRoles.RepeatDirection = 2
		'Test Caching:
		Dim rs
		Set rs = Nothing
		If Application("CHKROLES") = "" Then
			Set rs = GetRecordset("SELECT id, name from role")					
			Call CacheListItemsCollection(rs,"name","id","CHKROLES",False,"")
			Set rs = Nothing
		End If
		SetListItemsCollectionFromCache chkRoles,"CHKROLES"		
		
	End Function

	Public Function Page_Load()
	
		If Not Page.IsPostBack Then

			If Request.QueryString("id")<>"" Then
				recordID = Clng(Request.QueryString("id"))			
				 
			End if
			Call LoadRecord()
						
		End If	
	End Function
	
	Public Function Page_LoadViewState()
		recordID = Page.ViewState.GetValue("ID") 'Get from viewstate	
	

	End Function
	Public Function Page_PreRender()
		Page.ViewState.Add "ID",recordID 'Add to viewstate :-)
		
	End Function

	Public Function cmdSave_OnClick()
		Dim sSQL
		Dim rs
		Dim mode
		
		
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If
		
		sSQL = "SELECT * FROM [user] Where id=" & recordID
		
		
		Set rs = DBLayer.GetRecordSet(sSQL)
		
		
		If rs.RecordCount = 0 Then
			rs.AddNew
			mode = "new"
		End If
		
		rs("username").Value	= txtUsername.Text
		rs("firstname").Value    = txtFirstname.Text	
		rs("lastname").Value		= txtLastname.Text
		rs("email").Value	= txtEmail.Text
		rs("phone").Value	= txtPhone.Text
	
		rs("status").Value = chkStatus.Checked
		rs("isdeleted").Value = chkDelete.Checked 
		
		
	
		DBLayer.UpdateRecordSet rs
		If Mode = "new" Then
			'Get user  ID
			sSQL = "SELECT max(id) as id FROM [user]"			
			Set rs = DBLayer.GetRecordSet(sSQL)	
			recordID = rs("id").Value	
			
		End If
		
		'update to permission
		
		
		Dim x
			
		For x = 0 To chkRoles.Items.Count-1
			
			If chkRoles.Items.IsSelected(x) Then
				sSQL = "SELECT * from permission where userid= " & recordID & " and roleid = " &  chkRoles.Items.GetValue(x)
				Set rs = DBLayer.GetRecordSet(sSQL)
				If rs.RecordCount = 0 Then
					rs.AddNew					
				End If
				
				rs("roleid").Value = chkRoles.Items.GetValue(x)
				rs("userid").Value = recordID
				rs("access").Value = "all"
				rs("isdeleted").Value = false
				
				DBLayer.UpdateRecordSet rs
				'Response.Write(chkMenu.Items.GetText(x) & " Is Selected, Value:" &  chkMenu.Items.GetValue(x) & "<BR>")
			Else
				If Mode <> "new" Then
					DBLayer.ExecuteSQL "Delete  from permission where userid = " & recordID & "and roleid = " &  chkRoles.Items.GetValue(x)
				End If
			End If
		Next
		
		
		If mValidation.IsValid Then
			Response.Redirect "User.asp"
		End If
		
	End Function
	
	Public Function Validate()
		
		'You could use several approaches to validation. I normaly use this approach and modify the control output as appropriate
	
		'(object,strFriendlyName, intDataType, bolRequired, strDefaultValue)
		mValidation.Validate 	txtUsername,"Username", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	txtEmail,"Email", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	txtPhone,"Phone", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	txtFirstname,"Firstname", VALIDATION_DATATYPE_STRING, True , ""
		If txtRePassword.Text <> txtPassword.Text then
			
			mValidation.AddError("Retype password is incorrect")
			
		End if
		
		
	End Function

	Public Function LoadRecord()
		Dim sSQL
		Dim rs
		
		sSQL = "SELECT * FROM [user] Where id=" & recordID
		Set rs = GetRecordSet(sSQL)
		
		If rs.RecordCount > 0 Then
			txtUsername.Text = rs("username").Value
			txtFirstname.Text  = rs("firstname").Value
			txtLastname.Text = rs("lastname").Value
			txtEmail.Text = rs("email").Value
			txtPhone.Text = rs("phone").Value
			chkStatus.Checked = rs("status").Value
			chkDelete.Checked = rs("isdeleted").Value
		End If
		
		'Get data from permission
		sSQL = "SELECT * from permission where userid= " & recordID
		Set rs = GetRecordSet(sSQL)
		While Not rs.EOF
		
			chkRoles.Items.SetSelectedByValue rs("roleid").Value, True
			rs.MoveNext
		Wend
			
	End Function

	Public Function Panel_SetVisibility(e) 
		Panel.Control.Visible = Not Panel.Control.Visible
	End Function
%>


<%Function RenderPanel() %>
<%mValidation%>
	<table border=0 ID="Table1">
		<tr valign=top><td>Username </td><td><%txtUsername%></td></tr>
		<tr valign=top><td>Password</td><td><%txtPassword%></td></tr>
		<tr valign=top><td>Retype Password</td><td><%txtRePassword%></td></tr>
		<tr valign=top><td>Firstname</td><td><%txtFirstname%></td></tr>
		<tr valign=top><td>Lastname</td><td><%txtLastname%></td></tr>
		<tr valign=top><td>Email</td><td><%txtEmail%></td></tr>
		<tr valign=top><td>Phone</td><td><%txtPhone%></td></tr>
		
		<tr valign=top><td>Status</td><td><%chkStatus%></td></tr>
		<!--
		<tr valign=top><td>Deleted</td><td><%chkDelete%></td></tr>
		-->
		<tr valign=top><td>Roles</td><td><%chkRoles%></td></tr>
	
		<tr><td colspan=2 align=left><a href="User.asp"><HR>Back</a> | <%cmdSave%></td></tr>
	</table>
	
<%End Function%>
